﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.CropsProduct
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    Guid ProductID = new Guid(Request.Params["id"]);
                    BindCrops();
                    BindFactory();
                    ShowInfo(ProductID);
                }
            }
        }

        protected void BindCrops()
        {
            Things.BLL.Crops farmerBLL = new BLL.Crops();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = farmerBLL.GetList(strWhere.ToString());
            Things.Common.UICommon.BindDDL(ddlCrops, ds, "CropsName", "CropsID");
        }

        protected void BindFactory()
        {
            Things.BLL.Factory cropsCategoryBLL = new BLL.Factory();
            DataSet ds = new DataSet();
            ds = cropsCategoryBLL.GetAllList();
            Things.Common.UICommon.BindDDL(ddlFactory, ds, "FactoryName", "FactoryID");
        }

        private void ShowInfo(Guid ProductID)
        {
            Things.BLL.CropsProduct bll = new Things.BLL.CropsProduct();
            Things.Model.CropsProduct model = bll.GetModel(ProductID);
            this.lblProductID.Text = model.ProductID.ToString();            
            this.txtProductDate.Text = model.ProductDate.ToString("yyyy-MM-dd");
            Things.Common.UICommon.SetSelectedDDL(ddlCrops, model.CropsID.ToString());
            Things.Common.UICommon.SetSelectedDDL(ddlFactory, model.FactoryID.ToString());
        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";

            Guid cropsID = Guid.Empty;
            if (Guid.TryParse(this.ddlCrops.SelectedValue, out cropsID) == false)
            {
                strErr += "请选择农产品名称！\\n";
            }

            Guid factoryID = Guid.Empty;
            if (Guid.TryParse(this.ddlFactory.SelectedValue, out factoryID) == false)
            {
                strErr += "请选择加工厂！\\n";
            }
            if (!PageValidate.IsDateTime(txtProductDate.Text))
            {
                strErr += "加工时间格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtProductDate.Text))
            {
                strErr += "加工时间格式错误！\\n";
            }
            if (string.IsNullOrEmpty(this.txtProductionBatch.Text))
            {
                strErr += "请输入生产批次！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid ProductID = new Guid(this.lblProductID.Text);
            DateTime ProductDate = DateTime.Parse(this.txtProductDate.Text);
            string ProductionBatch = this.txtProductionBatch.Text;
            bool IsSaledOut = this.chkIsSaledOut.Checked;

            Things.BLL.CropsProduct bll = new Things.BLL.CropsProduct();
            Things.Model.CropsProduct model = bll.GetModel(ProductID);
            if (model != null)
            {
                model.CropsID = cropsID;
                model.FactoryID = factoryID;
                model.ProductDate = ProductDate;
                model.ProductionBatch = ProductionBatch;
                model.IsSaledOut = IsSaledOut;
                
                bll.Update(model);
                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
